package snafu.implementation.actions.walk.forward;

import java.util.ArrayList;
import java.util.List;

import snafu.framework.actions.Intention;
import snafu.framework.agents.AgentState;
import snafu.framework.objects.WorldObject;
import snafu.framework.worlds.Position;
import snafu.framework.worlds.Tile;
import snafu.framework.worlds.WorldMap;
import snafu.implementation.actions.walk.WalkAction;
import snafu.implementation.states.literals.direction.DirectionLiteral;
import snafu.implementation.states.literals.position.PositionLiteral;

public class WalkForwardAction extends WalkAction {

	public WalkForwardAction(List<WorldObject> targets, Intention<?> intention) {
		super(targets, intention);
	}

	@Override
	public void execute(WorldMap worldMap) {
		Position agentPosition = agent.getState().getLiteral(PositionLiteral.class);
		Integer direction = agent.getState().getLiteral(DirectionLiteral.class, 0);
		
		if (targets.size() == 0 || targets == null) {
			Tile frontTile = worldMap.getFrontTile(agentPosition, direction);
			for (WorldObject object : frontTile.getWorldObjects()) {
				if (intention.isApplicable(worldMap, (AgentState) agent.getState(), object)) {
					this.targets = new ArrayList<WorldObject>();
					targets.add(object);
					break;
				}
			}
		}
		
		// do the same as walking
		super.execute(worldMap);

	}

	@Override
	public String output() {
		return agent + " walks forward on " + targets.get(0);
	}

	@Override
	public boolean supportMultiTargets() {
		return false;
	}

}
